import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  DefaultValuePipe,
  UseGuards,
  SetMetadata,
} from '@nestjs/common';
import { CommunityService } from './community.service';
import { CreateCommunityDto } from './dto/create-community.dto';
import { UpdateCommunityDto } from './dto/update-community.dto';
import { CreatePostDto } from './dto/create-post.dto';
import { generateParseIntPipe } from 'src/utils';
import { CreateCommentDto } from './dto/create-comment.dto';
import { PermissionGuard } from 'src/permission.guard';
import { LoginGuard } from 'src/login.guard';
import { RequirePermission } from 'src/custom.decorator';

@Controller('community')
export class CommunityController {
  constructor(private readonly communityService: CommunityService) {}

  // 发布帖子
  @Post('post/create')
  async createPost(@Body() createPostDto: CreatePostDto) {
    return await this.communityService.createPost(createPostDto);
  }

  // 删除帖子
  @Delete('post/delete/:postId/:userId')
  async deletePost(
    @Param('postId') postId: number,
    @Param('userId') userId: number,
  ) {
    return await this.communityService.deletePost(postId, userId);
  }

  // 按名字模糊查询帖子
  @Get('post/list')
  async getPostByName(
    @Query('title') title: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return await this.communityService.getPostByName(title, pageNo, pageSize);
  }

  // 帖子详情
  @Get('post/detail/:postId/:userId')
  async getPostDetail(
    @Param('userId') userId: number,
    @Param('postId') postId: number,
  ) {
    return await this.communityService.getPostDetail(postId, userId);
  }

  // 用户评论帖子
  @Post('comment/create')
  async createComment(@Body() createCommentDto: CreateCommentDto) {
    return await this.communityService.createComment(createCommentDto);
  }

  // 点赞/取消点赞 帖子
  @Post('post/toggle')
  async togglePostLike(
    @Body('postId') postId: number,
    @Body('userId') userId: number,
  ) {
    return await this.communityService.togglePostLike(postId, userId);
  }

  // 点赞/取消点赞 评论
  @Post('comment/toggle')
  async toggleCommentLike(
    @Body('commentId') commentId: number,
    @Body('userId') userId: number,
  ) {
    return await this.communityService.toggleCommentLike(commentId, userId);
  }

  // 管理员删除帖子
  @Delete('post/adminDelete/:postId')
  async deletePostAdmin(@Param('postId') postId: number) {
    return await this.communityService.deletePostAdmin(Number(postId));
  }

  // 管理员删除评论
  @Delete('comment/adminDelete/:commentId')
  async deleteCommentAdmin(@Param('commentId') commentId: number) {
    return await this.communityService.deleteCommentAdmin(commentId);
  }

  // 管理员查看评论列表
  @Get('comment/list')
  async getCommentList(
    @Query('content') content: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return await this.communityService.getCommentList(
      content,
      pageNo,
      pageSize,
    );
  }
}
